package com.metaera.app.ui.activities;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.metaera.app.BuildConfig;
import com.metaera.app.LocalConfigStore;
import com.metaera.app.R;
import com.metaera.app.controller.presenter.VersionUpdatePresenter;
import com.metaera.app.controller.view.VersionUpdateDialogView;
import com.metaera.app.data.model.AppConfigResponse;
import com.metaera.app.ui.core.BaseActivity;
import com.metaera.app.utils.ActivityTitleHelper;
import com.metaera.app.utils.ToastUtils;
import com.metaera.app.utils.ViewUtils;

/**
 * 关于
 */
public class AboutActivity extends BaseActivity {

    public static final int ABOUT_TYPE = 1, CONTENT_TYPE = 2, BUSINESS_TYPE = 3;

    public static Intent newInstance(Context context, int type) {
        Intent intent = new Intent(context, AboutActivity.class);
        intent.putExtra("TYPE", type);
        return intent;
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_about;
    }

    @Override
    protected void initialize(Bundle savedInstanceState) {
        TextView tvVersionName = findViewById(R.id.tv_version_name);
        tvVersionName.setText("v " + BuildConfig.VERSION_NAME + "(" + BuildConfig.VERSION_CODE + ")");

        TextView tvTitle1 = findViewById(R.id.tv_title_1);
        TextView tvTitle2 = findViewById(R.id.tv_title_2);
        TextView tvContent1 = findViewById(R.id.tv_content_1);
        TextView tvContent2 = findViewById(R.id.tv_content_2);
        ImageView ivImage1 = findViewById(R.id.iv_image1);
        ImageView ivImage2 = findViewById(R.id.iv_image2);

        int type = getIntent().getIntExtra("TYPE", ABOUT_TYPE);

        AppConfigResponse config = LocalConfigStore.getInstance().getConfig();

        switch (type) {
            case ABOUT_TYPE:
                findViewById(R.id.layout).setVisibility(View.VISIBLE);
                ViewUtils.setVisibility(View.GONE, tvTitle1, tvTitle2);
                ivImage1.setImageResource(R.drawable.b_ic_about_arrow);
                ivImage2.setImageResource(R.drawable.b_ic_about_arrow);
                tvContent1.setText(R.string.about_appraisal);
                tvContent2.setText(R.string.about_version_update);
                ActivityTitleHelper.setTitle(this, R.string.about_app_text);

                findViewById(R.id.layout_1).setOnClickListener(v -> {
                    try {
                        Uri uri = Uri.parse("market://details?id=" + getPackageName());
                        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                    } catch (Exception e) {
                        ToastUtils.makeText("您的手机没有安装Android应用市场");
                        e.printStackTrace();
                    }
                });


                tvContent2.setOnClickListener(v -> {
                    ToastUtils.makeText("当前为最新版本");
                    VersionUpdatePresenter presenter = new VersionUpdatePresenter();
                    presenter.setModelAndView(new VersionUpdateDialogView(this));
                    getLifecycle().addObserver(presenter);
                    presenter.update();
                });

                if (config != null) {
                    findViewById(R.id.tv_terms).setOnClickListener(v ->
                            startActivity(WebViewActivity.newIntent(v.getContext(), config.getPrivacy_policy().getValue())));

                    findViewById(R.id.tv_protocol).setOnClickListener(v ->
                            startActivity(WebViewActivity.newIntent(v.getContext(), config.getTerm_of_service().getValue())));

                    findViewById(R.id.tv_disclaimer).setOnClickListener(v ->
                            startActivity(WebViewActivity.newIntent(v.getContext(), config.getDisclaimers().getValue())));
                }

                break;
            case CONTENT_TYPE:
                tvTitle1.setText(R.string.media_cooperation);
                tvTitle2.setText(R.string.market_cooperation);
                ActivityTitleHelper.setTitle(this, R.string.about_content_cooperate);

                if (config != null) {
                    tvContent1.setText(config.getMedia_cooperation().getValue());
                    tvContent2.setText(config.getMarket_cooperation().getValue());

                    ivImage1.setOnClickListener(v -> copy(tvContent1.getText().toString()));
                    ivImage2.setOnClickListener(v -> copy(tvContent2.getText().toString()));
                }
                break;
            case BUSINESS_TYPE:
                tvTitle1.setText(R.string.market_cooperation);
                tvTitle2.setText(R.string.cooperation);
                ActivityTitleHelper.setTitle(this, R.string.about_business_cooperate);

                AppConfigResponse config2 = LocalConfigStore.getInstance().getConfig();
                if (config2 != null) {
                    tvContent1.setText(config2.getMarket_cooperation().getValue());
                    tvContent2.setText(config2.getAdvertising_cooperation().getValue());

                    ivImage1.setOnClickListener(v -> copy(tvContent1.getText().toString()));
                    ivImage2.setOnClickListener(v -> copy(tvContent2.getText().toString()));

                }
                break;
        }

    }

    private void copy(String content) {
        ClipboardManager cm = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
        cm.setPrimaryClip(ClipData.newPlainText("Simple text", content));
        ToastUtils.makeText("复制成功");
    }

}
